﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace GoodDotNet.Service
{
    /// <summary>
    /// appsettings.json操作类
    /// </summary>
    public class AppsettingsService
    {
        static IConfiguration Configuration { get; set; }

        /// <summary>
        /// 配置文件
        /// </summary>
        public AppsettingsService()
        {
            string Path = $"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json";
            //string Path = "appsettings.json";
            Configuration = new ConfigurationBuilder()
                 .SetBasePath(Environment.CurrentDirectory)
                 .Add(new JsonConfigurationSource { Path = Path, Optional = false, ReloadOnChange = true })
                 .Build();
        }

        /// <summary>
        /// 封装要操作的字符
        /// </summary>
        /// <param name="sections">节点配置</param>
        /// <returns></returns>
        public static IConfiguration Get(String sections)
        {
            try
            {
                return Configuration.GetSection(sections);
            }
            catch (Exception) { }

            return null;
        }

        /// <summary>
        /// 封装要操作的字符
        /// </summary>
        /// <param name="sections">节点配置</param>
        /// <returns></returns>
        //public static T getValue<T>(string path)
        //{
        //    return Configuration.GetSection(path).Get<T>();

        //}

    }
}
